package infostroy.bookagolf.components.inputs;

import infostroy.bookagolf.UI.GolfRoundedButtonUI;
import infostroy.bookagolf.common.Constants.Colors;
import infostroy.bookagolf.common.Constants.Icons;
import infostroy.bookagolf.common.helpers.ImageHelper;
import infostroy.bookagolf.components.borders.RoundedBorder;
import infostroy.bookagolf.components.panels.GolfRightRoundPanel;
import infostroy.bookagolf.components.panels.ScrollPanel;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.plaf.basic.BasicComboBoxEditor;
import javax.swing.plaf.basic.BasicComboBoxUI;
import javax.swing.plaf.basic.BasicComboPopup;
import javax.swing.plaf.basic.ComboPopup;
import java.awt.*;
import java.awt.image.BufferedImage;

/**
 * Created by valer_000 on 1/30/14.
 */
public class GolfComboBox extends JComboBox implements Colors{

    private boolean isMaximumSet;
    private Dimension maximumSize;

    public GolfComboBox() {
        this.setSettings();
    }

    public GolfComboBox(Object[] items) {
        super(items);
        this.setSettings();
    }

    private void setSettings() {
        this.setEditable(true);
        this.setOpaque(false);
        this.setEditor(new GolfComboBoxEditor());
        this.setRenderer(new GolfComboBoxListCell());
        this.setBorder(new RoundedBorder());
        this.setUI(new GolfComboBoxUI());
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(super.getPreferredSize().width, 30);
    }

    @Override
    public Dimension getMaximumSize() {
        if (!isMaximumSet) {
            return new Dimension(super.getMaximumSize().width, 30);
        } else {
            return maximumSize;
        }
    }

    @Override
    public void setMaximumSize(Dimension maximumSize) {
        this.isMaximumSet = true;
        this.maximumSize = maximumSize;
    }

    public class GolfComboBoxUI extends BasicComboBoxUI {

        @Override
        protected JButton createArrowButton() {
            BufferedImage icon = ImageHelper.getInstance().loadImage(Icons.ARROW_DOWN);
            if (icon != null) {
                JButton arrowButton = new JButton(new ImageIcon(icon));
                arrowButton.setUI(new GolfRoundedButtonUI(false, true, false, true, true));
                arrowButton.setBorderPainted(false);
                arrowButton.setContentAreaFilled(false);
                return arrowButton;
            } else {
                return super.createArrowButton();
            }
        }

        @Override
        protected ComboPopup createPopup() {
            BasicComboPopup popup = new GolfComboPopup(comboBox);
            popup.setBorder(new LineBorder(INPUT_BORDER, 1, true));
            return popup;
        }
    }

    @Override
    public Object getSelectedItem() {
        return super.getSelectedItem();
    }

    protected class GolfComboBoxEditor extends BasicComboBoxEditor {
        private JPanel panel = new GolfRightRoundPanel();
        private JLabel label = new JLabel();
        private Object selectedItem;

        protected GolfComboBoxEditor() {
            label.setForeground(TABS_BACKGROUND);
            label.setFont(UIManager.getDefaults().getFont("TextField.font"));
            panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
            panel.setBorder(new EmptyBorder(0, 5, 0, 0));
            panel.add(label);
        }

        @Override
        public Component getEditorComponent() {
            return this.panel;
        }

        @Override
        public void setItem(Object anObject) {
            this.selectedItem = anObject;
            if (anObject != null) {
                this.label.setText(anObject.toString());
            }
        }

        @Override
        public Object getItem() {
            if (selectedItem != null)
                return selectedItem;
            else
                return null;
        }
    }

    private class GolfComboBoxListCell extends JPanel implements ListCellRenderer {

        private JLabel label = new JLabel();

        public GolfComboBoxListCell() {
            this.setLayout(new BorderLayout());
            this.setOpaque(true);
            this.setBorder(new EmptyBorder(5, 6, 5, 0));
            this.setBackground(Color.WHITE);
            this.add(label, BorderLayout.LINE_START);
        }

        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index
                , boolean isSelected, boolean cellHasFocus) {
            label.setText(value.toString());
            if (isSelected) {
                this.setBackground(TABS_BACKGROUND);
                label.setForeground(Color.WHITE);
            } else {
                this.setBackground(Color.WHITE);
                label.setForeground(Color.BLACK);
            }
            return this;
        }
    }

    private class GolfComboPopup extends BasicComboPopup {

        private GolfComboPopup(JComboBox combo) {
            super(combo);
        }

        @Override
        protected JScrollPane createScroller() {
            JScrollPane sp = new ScrollPanel(list,
                    ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
                    ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
            sp.setHorizontalScrollBar(null);
            return sp;
        }
    }
}
